from django.shortcuts import render,redirect
from django.http import HttpResponse
from myapp.models import Machines 
from myapp.models import MachinesUsage,Users
from django.core.urlresolvers import reverse
import os
# Create your views here.

def index(request):
    context2 = {}
    list  = Machines.objects.all()
    context2['list'] = list
    return render(request,"web/index.html",context2)
    #return HttpResponse("Hello, world. You're at the myapp index.")

def add(request):
    return render(request,"web/add.html")

def insert(request):
    #print('machinename:' + request.POST['machinename'])
    #print('machineip:' + request.POST['machineip'])
    name = request.POST['machinename']
    ip = request.POST['machineip']
    user =  request.POST['machineuser']
    password = request.POST['machinepassword']
    cmd = 'sshpass -p ' + password + ' ssh-copy-id ' + user + '@' + ip
    res = os.system(cmd)
    try:
        ob = Machines()
        ob.name = name
        ob.ip = ip
        ob.state = 0
        ob.user = user
        ob.save()
        if res == 0:
            context = {'info':"Add Successful.\n But please contact with Admin on SSH-KeyGen if the state of this machine is 'Disable' after 10 second"}
        else:
            context = {'info':"Add Successful.\n But SSH-KeyGen configuration is failed, please contact with Admin to do SSH-KeyGen configuration"}
    except Exception as err:
        print(err)
        context = {'info':"Add Failed"}
    
    return render(request,"web/info.html",context)

def delete(request,mid):
    try:
        context = {}
        machine = Machines.objects.get(id=mid)
        machine.delete()
        context['info'] = 'Delete Machine Successful'
    except Exception as err:
        print("err")
        context['info'] = 'Delete Machine Failed'
        
    return render(request,"web/info.html",context)


def view_usage(request,mid):
    try:
        list1 = MachinesUsage.objects.filter(m_id = mid)
        context = {'list':list1}
        return render(request,"web/detail.html",context)
    except Exception as err:
        context = {'info':'Open view page failed!'}
        print(err)
        return render(request,"web/info.html",context)


def login(request):
    return render(request,"web/login.html")

def dologin(request):
    try:
        ob = Users.objects.get(name=request.POST['username'])
        import hashlib
        m = hashlib.md5()
        m.update(bytes(request.POST['userpassword'],encoding="utf8"))
        if ob.password == m.hexdigest():
            request.session['adminuser'] = ob.name
            print(request.session['adminuser'])
            context = {'info':'Login Successful'}
            return render(request,"web/info.html",context)
            #return redirect(reverse('index'))
        else:
            context = {'info':'Wrong Password'}
            return render(request,"web/info.html",context)
    except Exception as err:
        print(err)
        context = {'info':'Login Failed. wrong user or password '}
        return render(request,"web/info.html",context)
    
def logout(request):
    del request.session['adminuser']
    context = {'info':'Logout Successful'}
    return render(request,"web/info.html",context)
    
